#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
#from datetime 
import datetime

class Voice(db.Model):
    when = db.DateTimeProperty(auto_now_add=True)


class MyHandler(webapp.RequestHandler):
    
    def get(self):
        voicesCount = [];
        for i in range(10):
            x_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=i * 10);
            plus_one_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=(i+1) * 10);
            voicesCount.append(
                        db.Query(Voice)
                        .filter('when > ', plus_one_minutes_ago)
                        .filter('when < ', x_minutes_ago)
                        .count());
                        
        values = {
            'voicesCount': voicesCount
        }
        self.response.out.write(template.render('prof.html', values))
    
    def post(self):
        voice = Voice()
        voice.put()
        self.redirect('/')
        self.response.out.write('posted!')  

def main():
    app = webapp.WSGIApplication([
        (r'.*', MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()